class Frame {
    source;
    size;
    halfsize;
    pivot;

    constructor(sourcex, sourcey, width, height) {
        this.init(sourcex, sourcey, width, height);
    }
    init(sourcex, sourcey, width, height) {
        this.source = { x: 0, y: 0 };
        this.size = { width: 0, height: 0 };
        this.halfsize = { width: 0, height: 0 };
        this.pivot = { x: 0, y: 0 };

        this.source.x = sourcex;
        this.source.y = sourcey;
        this.size.width = width;
        this.size.height = height;
        this.halfsize.width = Math.round(width / 2);
        this.halfsize.height = Math.round(height / 2);
    }

    setpivot(x, y) {
        this.pivot.x = Math.round(x); //convert to integer
        this.pivot.y = Math.round(y); //convert to integer
    }

    draw(targetX, targetY, img, angle, flipped, scale) {
        if (!scale) {
            scale = 1;
        }
        //angle is in degrees
        targetX = targetX | 0; //convert to integer
        targetY = targetY | 0;

        window.game.system.canvascontext.setTransform(1, 0, 0, 1, 0, 0); //reset transform to normal

        if (flipped === true) {
            window.game.system.canvascontext.translate(this.size.width, 0);
            window.game.system.canvascontext.scale(-1, 1);
            targetX *= -1;
        }

        if (angle === 0) {
            //drawImage(image, srcX, srcY, srcWidth, srch, dx, dy, dw, dh)
            window.game.system.canvascontext.drawImage(
                img, //image
                this.source.x, this.source.y, //source X,Y
                this.size.width, this.size.height, //source Size
                targetX, targetY, //target position
                this.size.width * scale, this.size.height * scale //target size
                );
        } else {
            window.game.system.canvascontext.save();
            window.game.system.canvascontext.translate((targetX + this.pivot.x + (this.halfsize.width * scale)),
                (targetY + this.pivot.y + (this.halfsize.height * scale)));

            //convert to radians then rotate
            window.game.system.canvascontext.rotate(angle * (0.0175427));

            window.game.system.canvascontext.drawImage(
                img, this.source.x, this.source.y, this.size.width, this.size.height,
                -(this.halfsize.width * scale) - this.pivot.x,
                -(this.halfsize.height * scale) - this.pivot.y,
                this.size.width * scale, this.size.height * scale
                );

            window.game.system.canvascontext.restore();
        }

        if (flipped === true) { //undo flipped transforms
            window.game.system.canvascontext.scale(-1, 1);
            window.game.system.canvascontext.translate(img.width * -1 * scale, 0);
        }
    }

    changesize(width, height) {
        if (width) {
            this.size.width = Math.max(width, 0);
            this.halfsize.width = width / 2;
        }

        if (height) {
            this.size.height = Math.max(height, 0);
            this.halfsize.height = height / 2;
        }
    }
} 